package edu.berkeley.cs160.GroupC.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import edu.berkeley.cs160.GroupC.AddSongActivity;
import edu.berkeley.cs160.GroupC.R;
import edu.berkeley.cs160.GroupC.Rating;
import edu.berkeley.cs160.GroupC.songs.Song;
import edu.berkeley.cs160.GroupC.views.PlaylistItem;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;

public class PlaylistAdapter extends BaseAdapter {

	private ArrayList<Song> playlist;
	private Context context;
	
	
	public PlaylistAdapter(ArrayList<Song> playlist, Context context) {
		super();
		this.playlist = playlist;
		this.context = context;
	}
	
	public int getCount() {
		return playlist.size();
	}

	public Object getItem(int position) {
		return (null==playlist)? null : playlist.get(position);
	}
	
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Log.v("ASA", "PlaylistAdapter: getView, start");
		PlaylistItem tli;
		if (convertView == null) {
			Log.v("ASA", "PlaylistAdapter: getView, convert view is null, start");
			tli = (PlaylistItem) View.inflate(context, R.layout.playlist_item, null);
			Log.v("ASA", "PlaylistAdapter: getView, convert view is null, end");
		}
		else {
			Log.v("ASA", "PlaylistAdapter: getView, convert view is not null, start");
			tli = (PlaylistItem) convertView;
			Log.v("ASA", "PlaylistAdapter: getView, convert view is not null, en d");
		}
		Log.v("ASA", "PlaylistAdapter: getView, tli add Song");
		tli.addSong(playlist.get(position));
		Log.v("ASA", "PlaylistAdapter: getView, end");
		return tli;
	}

	public void forceReload() {
		Log.v("ASA", "PlaylistAdapter: forceReload, start");
		notifyDataSetChanged();
		Log.v("ASA", "PlaylistAdapter: forceReload, end");
	}

	public void toggleAddedAtPosition(int position) {
		Log.v("ASA", "PlaylistAdapter: toggleAddedAtPosition, start");
		Song song = (Song)getItem(position);
		song.toggleInPlaylist();
		notifyDataSetChanged();
		Log.v("ASA", "PlaylistAdapter: toggleAddedAtPosition, end");
	}
	
	public void sortByComparator(Comparator<Song> c){
		Collections.sort(playlist, c);
		
	}

}